package com.txr.news.config;

import com.txr.news.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan({"com.txr.news.controller",
        "com.txr.news.controlleradvice",
        "com.txr.news.interceptor"})
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {
    //需要拦截器请先注入
    @Autowired
    AuthInterceptor authInterceptor;

    @Bean
    //上传文件解析器
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver cmr = new CommonsMultipartResolver();
        //设置上传文件的最大尺寸
        cmr.setMaxUploadSize(5*1024*1024);
        return cmr;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 向 springmvc 登记拦截什么请求，例如 /book 开始请求就拦截
        registry.addInterceptor(authInterceptor).addPathPatterns("/my/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**").addResourceLocations("/uploads/");
        //registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        //registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }
}
